import _ from 'lodash';
import moment from 'moment';

const Y = 'year';
const M = 'month';
const INCOME = 'income';
const DEVICE = 'device';
const GAMES = 'games';
const INCOME_TEMPLATE = {
    code: '',
    date: '',
    packageTotal: 0,
    rechargeTotal: 0,
};
const DEVICE_TEMPLATE = {
    code: '',
    date: '',
};
const GAME_TEMPLATE = {
    name: '',
    img: '',
    times: 0,
};

const initTemplate = ({ length = 0, date, template = {}, codeFix = 'report' }) => {
    const res = new Array(length);
    return _.map(res, (v, i) => {
        const item = { ...template };
        item.code = `${codeFix}_${i}`;
        if (item.date === '') {
            item.date = `${date}-${(i + 1 < 10) ? `0${i + 1}` : i + 1}`;
        }
        return item;
    });
};

export default ({ type, date, category, data = [], templateData = [] }) => {
    let templateType;
    let params;
    let template;
    switch (category) {
        case INCOME: {
            templateType = { ...INCOME_TEMPLATE };
            break;
        }
        case DEVICE: {
            templateType = { ...DEVICE_TEMPLATE };
            _.forEach(templateData, ({ id }) => {
                templateType[id] = 0;
            });
            break;
        }
        case GAMES: {
            templateType = { ...GAME_TEMPLATE };
            break;
        }
    }
    switch (type) {
        case Y: {
            params = {
                length: 12,
                date,
                template: templateType,
            };
            template = initTemplate(params);
            break;
        }
        case M: {
            params = {
                length: moment(date, 'YYYY-MM').daysInMonth(),
                date,
                template: templateType,
            };
            template = initTemplate(params);
            break;
        }
        case GAMES: {
            template = _.map(templateData, ({ id, name, imgs, category, remark }) => ({
                ...templateType,
                img: _.split(imgs, ',')[0],
                remark,
                category,
                id,
                name,
            }));
        }
    }
    _.forEach(data, (val) => {
        const { date, gameID } = val;
        let index;
        if (date) {
            index = _.findIndex(template, ['date', date]);
        }
        if (gameID) {
            index = _.findIndex(template, ['id', gameID]);
        }
        if (index > -1) {
            switch (category) {
                case INCOME: {
                    template[index] = { ...template[index], ...val };
                    break;
                }
                case DEVICE: {
                    const { deviceID, runCount } = val;
                    template[index] = { ...template[index], [deviceID]: runCount };
                    break;
                }
                case GAMES: {
                    const { runCount } = val;
                    template[index] = { ...template[index], times: runCount };
                    break;
                }
            }
        }
    });
    return template;
};
